/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package forcadistribuido;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author matheus
 */
public class InitClient {

    private static RemoteClient jr = null;
    private static String ENDERECO = "rmi://localhost/Forca";
    private static ForcaClientView view;

    public static void main( String args[] ) {  
        try {
            setJr(new RemoteClient(JOptionPane.showInputDialog(null, "Insira o seu apelido", "Registro", JOptionPane.INFORMATION_MESSAGE)));
            Registry reg = LocateRegistry.getRegistry("localhost");
	    IForcaRemota stub = (IForcaRemota) reg.lookup(ENDERECO);
            getJr().setRemoteServer(stub);
            UnicastRemoteObject.exportObject(getJr(),0);
            if(getJr().getRemoteServer().jogar(getJr())){
                getJr().setView(new ForcaClientView());
                getJr().getView().setTitle(getJr().getNomeJogador());
                getJr().getView().setVisible(true);
                getJr().getView().getjLabel1().setText(getJr().getRemoteServer().palavraSecreta());
            }
            
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(null, "O servidor forca não estão rodando",
                    "Erro de conexão", JOptionPane.ERROR_MESSAGE);
            int answer = JOptionPane.showOptionDialog(null, "Deseja tentar novamente?",
                    "Nova tentativa", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE
                    , null, null, null);
            if(answer == JOptionPane.YES_OPTION){
                InitClient.main(null);
            }
        } catch (NotBoundException ex) {
            Logger.getLogger(InitClient.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

    /**
     * @return the jr
     */
    public static RemoteClient getJr() {
        return jr;
    }

    /**
     * @param aJr the jr to set
     */
    public static void setJr(RemoteClient aJr) {
        jr = aJr;
    }

    /**
     * @return the ENDERECO
     */
    public static String getENDERECO() {
        return ENDERECO;
    }

    /**
     * @param aENDERECO the ENDERECO to set
     */
    public static void setENDERECO(String aENDERECO) {
        ENDERECO = aENDERECO;
    }

}
